package ai.leinao.alarm.notify;

import ai.leinao.notify.core.AbstractNotifierRegistry;
import ai.leinao.notify.core.NotifyProperties;
import ai.leinao.notify.core.enums.NotifyType;
import ai.leinao.notify.core.provider.NotifierProvider;
import ai.leinao.notify.dingtalk.enums.DingTalkProviderEnum;
import ai.leinao.notify.email.EmailProviderEnum;
import ai.leinao.notify.webhook.WebHookProviderEnum;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于数据库的通知器注册
 *
 * @author zhong.wang
 * @date 2025/9/9
 **/
@Component
public class DBNotifierRegistry  extends AbstractNotifierRegistry implements BeanPostProcessor {
    @Override
    protected NotifyProperties getNotifyProperties(NotifyType notifyType, String id) {
        if(notifyType == NotifyType.EMAIL && "dbAlarmEmail".equals(id)){
            Map<String,Object> configuration = new HashMap<>();
            configuration.put("host", "smtp.163.com");
            configuration.put("port", "465");
            configuration.put("username", "18834182912@163.com");
            configuration.put("password", "URfBCGV77pPCcpSs");
            configuration.put("protocol", "smtps");
            configuration.put("sender","dijia");
            configuration.put("ssl", true);
            NotifyProperties alertProperties = new NotifyProperties();
            alertProperties.setId("dbAlarmEmail");
            alertProperties.setProvider(EmailProviderEnum.EMAIL.getType());
            alertProperties.setName("dbAlarmEmail");
            alertProperties.setNotifyType(NotifyType.EMAIL);
            alertProperties.setConfiguration(configuration);
            return alertProperties;
        }
        if(notifyType == NotifyType.HTTP && "dbAlarmHttp".equals(id)){
            Map<String,Object> configuration = new HashMap<>();
            configuration.put("url", "http://localhost:9090/alert/receive");
            configuration.put("method", "POST");
            Map<String,String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            configuration.put("headers", headers);
            NotifyProperties alertProperties = new NotifyProperties();
            alertProperties.setId("dbAlarmHttp");
            alertProperties.setProvider(WebHookProviderEnum.WEBHOOK.getType());
            alertProperties.setName("dbAlarmHttp");
            alertProperties.setNotifyType(NotifyType.HTTP);

            alertProperties.setConfiguration(configuration);
            return alertProperties;
        }
        if(notifyType == NotifyType.DING_TALK && "dbAlarmDingTalk".equals(id)){
            Map<String,Object> configuration = new HashMap<>();
            configuration.put("url", "https://oapi.dingtalk.com/robot/send?access_token=21daaf09fd27d7b9b9a8d1ca7a0e806253c04b99238d7f6131aa4f860457400e");
            configuration.put("secret", "SEC578f3f8f963248542ba8792f4882c9d435b568a2c5b2d1282413e6eca19ff4a8");
            NotifyProperties alertProperties = new NotifyProperties();
            alertProperties.setId("dbAlarmDingTalk");
            alertProperties.setProvider(DingTalkProviderEnum.DING_TALK_ROBOT.getType());
            alertProperties.setName("dbAlarmDingTalk");
            alertProperties.setNotifyType(NotifyType.DING_TALK);

            alertProperties.setConfiguration(configuration);
            return alertProperties;
        }
        return null;

    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof NotifierProvider) {
            this.registerProvider((NotifierProvider) bean);
        }
        return bean;
    }

}
